//
//  CXNetworkTool.m
//  yichangxing
//
//  Created by 小宇 on 2017/7/15.
//  Copyright © 2017年 cxkj. All rights reserved.
//

#import "CXNetworkTool.h"
#import "AFNetworking.h"
//#import "CXMainViewController.h"
@implementation CXNetworkTool{
	AFHTTPSessionManager *_manager;
}

static id _instance;
+(instancetype)sharedTool{
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		_instance = [[self alloc] init];
	});
	return _instance;
}

-(instancetype)init{
	self = [super init];
	if(self){
		NSURL *url = [NSURL URLWithString:@"https://www.jiugeji.com/www/"];
		_manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
	}
	return self;
}

-(void)getRequestWithURL:(NSString *)urlString parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
	[SVProgressHUD show];
	[_manager GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		if(completeBlock){
			[SVProgressHUD dismiss];
			completeBlock(responseObject,nil);
		}
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		[SVProgressHUD dismiss];
		NSLog(@"%@",error);
		//		if(completeBlock){
		//			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
		//				switch (netState) {
		//					case 1:{
		//						NSLog(@"手机流量上网");
		//					}
		//						break;
		//					case 2:{
		//						NSLog(@"WIFI上网");
		//					}
		//						break;
		//					default:{
		//						NSLog(@"没网");
		//					}
		//						break;
		//				}
		//			}];
		//			completeBlock(nil,error);
		//		}
	}];
}

-(void)postRequestWithURL:(NSString *)urlString parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
	[SVProgressHUD show];
	[_manager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		if(completeBlock){
			[SVProgressHUD dismiss];
			completeBlock(responseObject,nil);
		}
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		[SVProgressHUD dismiss];
		NSLog(@"%@",error);
//		if(completeBlock){
//			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
//				switch (netState) {
//					case 1:{
//						NSLog(@"手机流量上网");
//					}
//						break;
//					case 2:{
//						NSLog(@"WIFI上网");
//					}
//						break;
//					default:{
//						NSLog(@"没网");
//					}
//						break;
//				}
//			}];
//			completeBlock(nil,error);
//		}
	}];
}

//-(void)postRequestWithURLApplication:(NSString *)urlString parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
//
//	NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//	NSString *token = [user objectForKey:@"token"];
//	_manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//	[_manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//	[_manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
//	[SVProgressHUD show];
//	[_manager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//		if(completeBlock){
//			//[self  performSelector:@selector(delayMethod) withObject:nil afterDelay:0.5f];
//			[SVProgressHUD dismiss];
//			completeBlock(responseObject,nil);
//		}
//	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//		[SVProgressHUD dismiss];
//		if(completeBlock){
//			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
//				switch (netState) {
//					case 1:{
//						NSLog(@"手机流量上网");
//					}
//						break;
//					case 2:{
//						NSLog(@"WIFI上网");
//					}
//						break;
//					default:{
//						NSLog(@"没网");
//					}
//						break;
//				}
//			}];
//			completeBlock(nil,error);
//		}
//	}];
//}
//
//-(void)postRequestWithURLREFRESH:(NSString *)urlString parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
//
//	NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//	NSString *token = [user objectForKey:@"token"];
//	_manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//	[_manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//	[_manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
//	[_manager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//		if(completeBlock){
//			completeBlock(responseObject,nil);
//		}
//	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//		if(completeBlock){
////			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
////				switch (netState) {
////					case 1:{
////						NSLog(@"手机流量上网");
////					}
////						break;
////					case 2:{
////						NSLog(@"WIFI上网");
////					}
////						break;
////					default:{
////						NSLog(@"没网");
////					}
////						break;
////				}
////			}];
////			completeBlock(nil,error);
////		}
//	}];
//}
//
//
//-(void)delayMethod{
//	[CXSVProgressHUD dismiss];
//}
//
//-(void)getRequestWithURLApplication:(NSString *)urlString parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
//
//	NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//	NSString *token = [user objectForKey:@"token"];
//
//	_manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//	//[_manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//	[_manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
//	//NSLog(@"传递的参数是：%@",params);
//	[CXSVProgressHUD show];
//	[_manager GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//		if(completeBlock){
//			//[self  performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];
//			[CXSVProgressHUD dismiss];
//			completeBlock(responseObject,nil);
//		}
//	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//		[CXSVProgressHUD dismiss];
//		if(completeBlock){
//			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
//				switch (netState) {
//					case 1:{
//						NSLog(@"手机流量上网");
//					}
//						break;
//					case 2:{
//						NSLog(@"WIFI上网");
//					}
//						break;
//					default:{
//						NSLog(@"没网");
//					}
//						break;
//				}
//			}];
//			completeBlock(nil,error);
//		}
//	}];
//}
//
//-(void)postRequestWithPhoto:(NSString *)urlString :(NSData *)data parameters:(NSDictionary *)params complete:(void(^)(id responseObject, NSError *error))completeBlock{
//	NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//	NSString *token = [user objectForKey:@"token"];
//	_manager.requestSerializer = [AFJSONRequestSerializer serializer];
//	[_manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//	[_manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
//	[CXSVProgressHUD show];
//	[_manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//		[formData appendPartWithFileData:data name:@"file" fileName:@"aaa.jpg" mimeType:@"image/jpg"];
//	} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//		if(completeBlock){
//			[CXSVProgressHUD dismiss];
//			completeBlock(responseObject,nil);
//		}
//	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//		[CXSVProgressHUD dismiss];
//		if(completeBlock){
//			[CXNetWorkUtils netWorkState:^(NSInteger netState) {
//				switch (netState) {
//					case 1:{
//						NSLog(@"手机流量上网");
//					}
//						break;
//					case 2:{
//						NSLog(@"WIFI上网");
//					}
//						break;
//					default:{
//						NSLog(@"没网");
//					}
//						break;
//				}
//			}];
//			completeBlock(nil,error);
//		}
//	}];
//}
@end
